package co.yixiang.hw.service.impl;

import co.yixiang.exception.BadRequestException;
import co.yixiang.hw.domain.YxStoreProduct;
import co.yixiang.hw.repository.YxStoreProductRepository;
import co.yixiang.hw.service.YxStoreProductService;
import co.yixiang.hw.service.dto.YxStoreProductDto;
import co.yixiang.hw.service.dto.YxStoreProductQueryCriteria;
import co.yixiang.hw.service.mapper.YxStoreProductMapper;
import co.yixiang.utils.FileUtil;
import co.yixiang.utils.PageUtil;
import co.yixiang.utils.QueryHelp;
import co.yixiang.utils.ValidationUtil;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;

/**
* @author Walert
* @date 2020-02-13
*/
@Service
//@CacheConfig(cacheNames = "yxStoreProduct")
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class YxStoreProductServiceImpl implements YxStoreProductService {

    @Resource
    private  YxStoreProductRepository yxStoreProductRepository;
    @Resource
    private  YxStoreProductMapper yxStoreProductMapper;

    @Override
    //@Cacheable
    public Map<String,Object> queryAll(YxStoreProductQueryCriteria criteria, Pageable pageable){

        Page<YxStoreProduct> page = yxStoreProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPageHadTotalPage(page.getContent(),page.getTotalElements(),page.getTotalPages());
    }

    @Override
    //@Cacheable
    public List<YxStoreProductDto> queryAll(YxStoreProductQueryCriteria criteria){
        return yxStoreProductMapper.toDto(yxStoreProductRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    //@Cacheable(key = "#p0")
    public YxStoreProductDto findById(Integer id) {
        YxStoreProduct yxStoreProduct = yxStoreProductRepository.findById(id).orElseGet(YxStoreProduct::new);
        ValidationUtil.isNull(yxStoreProduct.getId(),"YxStoreProduct","id",id);
        return yxStoreProductMapper.toDto(yxStoreProduct);
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public YxStoreProductDto create(YxStoreProduct resources) {
        return yxStoreProductMapper.toDto(yxStoreProductRepository.save(resources));
    }

    @Override
    //@CacheEvict(allEntries = true)
    @Transactional(rollbackFor = Exception.class)
    public void update(YxStoreProduct resources) {
        YxStoreProduct yxStoreProduct = yxStoreProductRepository.findById(resources.getId()).orElseGet(YxStoreProduct::new);
        ValidationUtil.isNull( yxStoreProduct.getId(),"YxStoreProduct","id",resources.getId());
        yxStoreProduct.copy(resources);
        yxStoreProductRepository.save(yxStoreProduct);
    }

    @Override
    //@CacheEvict(allEntries = true)
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            yxStoreProductRepository.deleteById(id);
        }
    }

    /**
     * 根据Ids查找商品
     * 检查商品是否可用
     * @param ids
     * @return
     */
    @Override
    public List<YxStoreProduct> findByIds(List<Integer> ids) {
        List<YxStoreProduct> products = yxStoreProductRepository.findByIdIn(ids);
        for (YxStoreProduct yxStoreProduct : products) {
            if(yxStoreProduct.getIsDel() == 1) throw new BadRequestException("【"+yxStoreProduct.getStoreName()+"】不存在");
            if(yxStoreProduct.getIsShow() == 0) throw new BadRequestException("【"+yxStoreProduct.getStoreName()+"】已下架");
            if(yxStoreProduct.getStock() <= 0) throw new BadRequestException("【"+yxStoreProduct.getStoreName()+"】库存不足");
        }
        return products;
    }
}
